package org.example.business.endpoint.http.server;

import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.example.business.endpoint.http.HttpSocket;
import org.example.business.endpoint.http.HttpSocketManager;
import org.example.business.endpoint.http.event.connection.SocketLifeCycleEvent;
import org.example.business.endpoint.http.event.message.HttpMsg;
import org.example.objevent.event.Event;
import org.example.objevent.event.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Sharable
@Component
public class HttpMessageHandler extends ChannelDuplexHandler {

    @Autowired
    private EventBus bus;

    @Autowired
    private HttpSocketManager socketManager;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("处理消息 [{}]", msg);

        if(!(msg instanceof HttpMsg)){
            super.channelRead(ctx, msg);
            return;

        }

        HttpMsg httpMsg = (HttpMsg)msg;

        httpMsg.setMsgId(String.valueOf(System.nanoTime()));
        httpMsg.setSocketId(socketManager.getSocket(ctx).getSocketKey());

        bus.publish(new Event("HTTP_REQ", httpMsg));

    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        log.info("registered {}", ctx);

        SocketLifeCycleEvent lifeCycleEvent = new SocketLifeCycleEvent();

        lifeCycleEvent.setType(SocketLifeCycleEvent.Type.CONNECT);
        lifeCycleEvent.setSocket(new HttpSocket(ctx));

        bus.publish(new Event("HTTP_LIFE_CYCLE", lifeCycleEvent));

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        log.info("unregistered {}", ctx);

        SocketLifeCycleEvent lifeCycleEvent = new SocketLifeCycleEvent();

        lifeCycleEvent.setType(SocketLifeCycleEvent.Type.CLOSE);
        lifeCycleEvent.setSocket(new HttpSocket(ctx));

        bus.publish(new Event("HTTP_LIFE_CYCLE", lifeCycleEvent));

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("active {}", ctx);

//        SocketLifeCycleEvent lifeCycleEvent = new SocketLifeCycleEvent();
//
//        lifeCycleEvent.setType(SocketLifeCycleEvent.Type.CONNECT);
//        lifeCycleEvent.setSocket(new HttpSocket(ctx));
//
//        bus.publish(new Event("HTTP_LIFE_CYCLE", lifeCycleEvent));

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("inactive {}", ctx);

//        SocketLifeCycleEvent lifeCycleEvent = new SocketLifeCycleEvent();
//
//        lifeCycleEvent.setType(SocketLifeCycleEvent.Type.CLOSE);
//        lifeCycleEvent.setSocket(new HttpSocket(ctx));
//
//        bus.publish(new Event("HTTP_LIFE_CYCLE", lifeCycleEvent));

    }


}
